<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\admin\logic;

use app\common\model\LotteryType;

/**
 * 开奖结果逻辑
 */
class LotteryResult extends AdminBase
{
    

    
    /**
     * 获取开奖结果列表
     */
    public function getLotteryResultList($where = [], $field = 'a.*,m.nickname,c.name as category_name', $order = '')
    {
        
        $this->modelLotteryResult->alias('a');
        
        $join = [

                    [ '__LOTTERY__ c', 'a.lottery_id = c.id'],
                ];

        $where['a.' . DATA_STATUS_NAME] = ['neq', DATA_DELETE];

        $this->modelLotteryResult->join = $join;
        $list=$this->modelLotteryResult->getList($where, $field, $order);

        $this->modelLotteryResult->alias('a');
        $count=$this->modelLotteryResult->join('__LOTTERY__ c', 'a.lottery_id = c.id')->where($where)->count();
        return ['list'=>$list,'count'=>$count];
    }
    
    /**
     * 获取开奖结果列表搜索条件
     */
    public function getWhere($data = [])
    {
        
        $where = [];
        
        !empty($data['search_data']) && $where['a.round'] = $data['search_data'];
        !empty($data['lottery_id']) &&   $where['a.lottery_id'] =$data['lottery_id'];

        return $where;
    }
    
    /**
     * 开奖结果信息编辑
     */
    public function LotteryResultEdit($data = [])
    {


        $url = url('LotteryResultList');
        if(empty($data['id'])){
            $info=$this->modelLotteryResult->getInfo(['round'=>$data['round'],'lottery_id'=>$data['lottery_id']]);
            if($info){
                return [RESULT_ERROR, '该期数已开奖'];
            }
            $data['source']='手动添加';

        }
        if(!explode(',',$data['number'])){
            return [RESULT_ERROR, '开奖号码格式错误'];
        }


        if(empty($data['create_time'])){
            $data['create_time']=time();
        }else{
            $data['create_time']=strtotime($data['create_time']);
        }

        $result = $this->modelLotteryResult->setInfo($data);
        
        $handle_text = empty($data['id']) ? '新增' : '编辑';
        
        $result && action_log($handle_text, '开奖结果' . $handle_text . '，期号：' . $data['round']);
        
        return $result ? [RESULT_SUCCESS, '开奖结果操作成功', $url] : [RESULT_ERROR, $this->modelLotteryResult->getError()];
    }

    /**
     * 获取开奖结果信息
     */
    public function getLotteryResultInfo($where = [], $field = 'a.*,c.name as category_name')
    {
        
        $this->modelLotteryResult->alias('a');

        $join = [

            [ '__LOTTERY__ c', 'a.lottery_id = c.id'],
        ];
        
        $where['a.' . DATA_STATUS_NAME] = ['neq', DATA_DELETE];
        
        $this->modelLotteryResult->join = $join;
        
        return $this->modelLotteryResult->getInfo($where, $field);
    }
    

    

    
    /**
     * 开奖结果删除
     */
    public function LotteryResultDel($where = [])
    {
        
        $result = $this->modelLotteryResult->deleteInfo($where);
        
        $result && action_log('删除', '开奖结果删除，where：' . http_build_query($where));
        
        return $result ? [RESULT_SUCCESS, '开奖结果删除成功'] : [RESULT_ERROR, $this->modelLotteryResult->getError()];
    }

    /**
     * 开奖结果删除
     */
    public function update_info($where = [])
    {

        //dd($where);

        $result = $this->modelLotteryResult->setInfo($where);


        return $result ? [RESULT_SUCCESS, '操作成功'] : [RESULT_ERROR, '操作失败'];
    }
}
